#!/bin/bash

# 配置目标 URL
URL="http://172.22.83.18:32199/hello"

# 压测参数
NUM_REQUESTS=1000     # 总请求数
NUM_THREADS=10        # 并发线程数

# 每个线程执行的请求数
REQUESTS_PER_THREAD=$((NUM_REQUESTS / NUM_THREADS))

# 用于计时
START_TIME=$(date +%s)

# 定义并发任务函数
send_requests() {
  for i in $(seq 1 $REQUESTS_PER_THREAD); do
    # 发送 GET 请求并获取状态码
    STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}" $URL)
    echo "Status Code: $STATUS_CODE"
  done
}

# 启动并发任务
for i in $(seq 1 $NUM_THREADS); do
  send_requests &
done

# 等待所有后台任务完成
wait

# 计算耗时和 QPS
END_TIME=$(date +%s)
TOTAL_TIME=$((END_TIME - START_TIME))
QPS=$(echo "scale=2; $NUM_REQUESTS / $TOTAL_TIME" | bc)

# 输出结果
echo "Total Requests: $NUM_REQUESTS"
echo "Total Time: $TOTAL_TIME seconds"
echo "QPS: $QPS"
